package com.justyh.ssm.controller;

import com.justyh.ssm.model.Place;
import net.sf.json.JSONObject;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.net.URL;
import java.util.*;

/**
 * Created by yh on 2016/9/17.
 */
@Controller
@RequestMapping("/place")
public class PlaceController {
//    @Resource
//    private PlaceService placeService;
    private List<Place> placeList;
    @RequestMapping(value = "/insertAll",method = RequestMethod.POST)
    @ResponseBody
    public Object insertAllPlaceFromWeb(HttpServletRequest request){
        String sourceurl=request.getParameter("sourceurl");
        try{
            JSONObject jsonObject = JSONObject.fromObject(getPlaces(sourceurl));
            return jsonObject;
        }catch (Exception e){
            e.printStackTrace();
            return "fail";
        }
    }
    //默认"http://www.stats.gov.cn/tjsj/tjbz/tjyqhdmhcxhfdm/2015"
    public  Map getPlaces(String sourceurl) throws IOException {
        placeList=new ArrayList<Place>();
        Map provinceMap=new LinkedHashMap();
//        Document doc= Jsoup.connect(sourceurl)
//                .userAgent("Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.2.15)")
//                .timeout(50000).get();
        Document doc=Jsoup.parse(readHtml(sourceurl));
        Elements provinces=doc.getElementsByClass("provincetr");
        for(Element provinceElement:provinces){
            Elements provinceSelect=provinceElement.select("a");
            for(Element province:provinceSelect){
                if(province.text().endsWith("市")){
                  provinceMap.put(province.text().replace("市","").trim(),parseCity(province,sourceurl));
                }else if(province.text().endsWith("省")){
                  provinceMap.put(province.text().replace("省","").trim(), parseCity(province,sourceurl));
                }else if("内蒙古自治区".equals(province.text())){
                    provinceMap.put(province.text().replace("自治区","").trim(), parseCity(province,sourceurl));
                }else{
                    provinceMap.put(province.text().substring(0,2), parseCity(province,sourceurl));
                }
            }
        }
       /* //start
        for (Object o : provinceMap.keySet()) {
            if(o.toString().endsWith("市")){
                Map cityMap=(Map)provinceMap.get(o);
                List<String> countys=new ArrayList<String>();
                for(Object s:cityMap.keySet()){
                    countys.addAll((List)cityMap.get(s));
                }
                cityMap.clear();
                cityMap.put(o.toString().replace("市","").trim(),countys);
                provinceMap.put(o,cityMap);
            }
        }//end*/
        //通过迭代器来修改元素
       /* Iterator<Map.Entry<String,Map>> it=provinceMap.entrySet().iterator();
        while(it.hasNext()){
            Map.Entry<String,Map> entry=it.next();
            String key=entry.getKey();
            if(key.endsWith("市")){
                provinceMap.put(key.replace("市","").trim(),entry.getValue());
                provinceMap.remove(key);
                it.remove();
            }
        }*/
        return provinceMap;

    }
    public  Map parseCity(Element parseElement,String baseUrl) throws IOException {
        Map cityMap=new LinkedHashMap();
//        Document document=Jsoup.connect(parseElement.attr("abs:href")).userAgent("Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.2.15)")
//                .timeout(50000).get();
       // Document document=Jsoup.parse(new URL(parseElement.attr("abs:href")).openStream(), "GBK",baseUrl);
        Document document=Jsoup.parse(readHtml(baseUrl+"/"+parseElement.attr("href")));
        Elements cityElements=document.getElementsByClass("citytr");

        for(int i=0;i<cityElements.size();i++){
            List<String> countyList= parseCounty(cityElements.get(i).select("a").last(),baseUrl);
            if(cityElements.get(i).select("a").last().text().toString().indexOf("直辖县级行政区划")!=-1){
                for(String s:countyList){
                    List<String> list=new ArrayList<String>();
                    list.add(s);
                    cityMap.put(s,list);
                }
            }else{
                cityMap.put(cityElements.get(i).select("a").last().text().replace("市","").trim(),countyList);
            }
        }

        if(parseElement.text().endsWith("市")){
            List<String> countys=new ArrayList<String>();
            for(Object s:cityMap.keySet()){
                countys.addAll((List)cityMap.get(s));
            }
            cityMap.clear();
            cityMap.put(parseElement.text().replace("市","").trim(),countys);
        }
        return cityMap;

    }
    public  List<String> parseCounty(Element element,String baseUtl) throws IOException {
        List<String> countyList=new ArrayList<String>();
//        Document document = Jsoup.connect(element.attr("abs:href")).userAgent("Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.2.15)")
//                .timeout(50000).get();
        Document document=Jsoup.parse(readHtml(baseUtl+"/"+element.attr("href")));
        Elements countyElements = document.getElementsByClass("countytr");
        if(countyElements.size()>0){
            for (int i=0;i<countyElements.size();i++) {
                if(countyElements.get(i).select("a").last()!=null){
                    countyList.add(countyElements.get(i).select("a").last().text());
                }
            }
        }else{
            return parseTown(element,baseUtl);
        }
        return countyList;
    }

    public  List<String> parseTown(Element element,String baseUrl) throws IOException {
        List<String> townList=new ArrayList<String>();
        Document document=Jsoup.parse(readHtml(baseUrl+"/"+element.attr("href")));
        Elements townElements = document.getElementsByClass("towntr");
        for(Element town:townElements){
            townList.add(town.select("td").last().text());
        }
        return townList;
    }

    /**
     * 处理读取网页的编码格式
     * @param myurl
     * @return
     */
    public static String readHtml(String myurl) {
        StringBuffer sb = new StringBuffer("");
        URL url;
        try {
            url = new URL(myurl);
            BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream(), "gbk"));
            String s = "";
            while ((s = br.readLine()) != null) {
                sb.append(s + "\r\n");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return sb.toString();
    }
}